/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ep.persistence.controllers;

import ep.persistence.controllers.exceptions.NonexistentEntityException;
import ep.persistence.entities.T003Pharmacist;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import ep.persistence.entities.T001User;
import ep.persistence.entities.T006Prescription;
import java.util.ArrayList;
import java.util.Collection;

/**
 *
 * @author Spyros
 */
public class T003PharmacistJpaController {

    public T003PharmacistJpaController() {
        emf = Persistence.createEntityManagerFactory("EprescPersistencePU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(T003Pharmacist t003Pharmacist) {
        if (t003Pharmacist.getT006PrescriptionCollection() == null) {
            t003Pharmacist.setT006PrescriptionCollection(new ArrayList<T006Prescription>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            T001User t001User = t003Pharmacist.getT001User();
            if (t001User != null) {
                t001User = em.getReference(t001User.getClass(), t001User.getId());
                t003Pharmacist.setT001User(t001User);
            }
            Collection<T006Prescription> attachedT006PrescriptionCollection = new ArrayList<T006Prescription>();
            for (T006Prescription t006PrescriptionCollectionT006PrescriptionToAttach : t003Pharmacist.getT006PrescriptionCollection()) {
                t006PrescriptionCollectionT006PrescriptionToAttach = em.getReference(t006PrescriptionCollectionT006PrescriptionToAttach.getClass(), t006PrescriptionCollectionT006PrescriptionToAttach.getId());
                attachedT006PrescriptionCollection.add(t006PrescriptionCollectionT006PrescriptionToAttach);
            }
            t003Pharmacist.setT006PrescriptionCollection(attachedT006PrescriptionCollection);
            em.persist(t003Pharmacist);
            if (t001User != null) {
                t001User.getT003PharmacistCollection().add(t003Pharmacist);
                t001User = em.merge(t001User);
            }
            for (T006Prescription t006PrescriptionCollectionT006Prescription : t003Pharmacist.getT006PrescriptionCollection()) {
                T003Pharmacist oldT003PharmacistOfT006PrescriptionCollectionT006Prescription = t006PrescriptionCollectionT006Prescription.getT003Pharmacist();
                t006PrescriptionCollectionT006Prescription.setT003Pharmacist(t003Pharmacist);
                t006PrescriptionCollectionT006Prescription = em.merge(t006PrescriptionCollectionT006Prescription);
                if (oldT003PharmacistOfT006PrescriptionCollectionT006Prescription != null) {
                    oldT003PharmacistOfT006PrescriptionCollectionT006Prescription.getT006PrescriptionCollection().remove(t006PrescriptionCollectionT006Prescription);
                    oldT003PharmacistOfT006PrescriptionCollectionT006Prescription = em.merge(oldT003PharmacistOfT006PrescriptionCollectionT006Prescription);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(T003Pharmacist t003Pharmacist) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            T003Pharmacist persistentT003Pharmacist = em.find(T003Pharmacist.class, t003Pharmacist.getId());
            T001User t001UserOld = persistentT003Pharmacist.getT001User();
            T001User t001UserNew = t003Pharmacist.getT001User();
            Collection<T006Prescription> t006PrescriptionCollectionOld = persistentT003Pharmacist.getT006PrescriptionCollection();
            Collection<T006Prescription> t006PrescriptionCollectionNew = t003Pharmacist.getT006PrescriptionCollection();
            if (t001UserNew != null) {
                t001UserNew = em.getReference(t001UserNew.getClass(), t001UserNew.getId());
                t003Pharmacist.setT001User(t001UserNew);
            }
            Collection<T006Prescription> attachedT006PrescriptionCollectionNew = new ArrayList<T006Prescription>();
            for (T006Prescription t006PrescriptionCollectionNewT006PrescriptionToAttach : t006PrescriptionCollectionNew) {
                t006PrescriptionCollectionNewT006PrescriptionToAttach = em.getReference(t006PrescriptionCollectionNewT006PrescriptionToAttach.getClass(), t006PrescriptionCollectionNewT006PrescriptionToAttach.getId());
                attachedT006PrescriptionCollectionNew.add(t006PrescriptionCollectionNewT006PrescriptionToAttach);
            }
            t006PrescriptionCollectionNew = attachedT006PrescriptionCollectionNew;
            t003Pharmacist.setT006PrescriptionCollection(t006PrescriptionCollectionNew);
            t003Pharmacist = em.merge(t003Pharmacist);
            if (t001UserOld != null && !t001UserOld.equals(t001UserNew)) {
                t001UserOld.getT003PharmacistCollection().remove(t003Pharmacist);
                t001UserOld = em.merge(t001UserOld);
            }
            if (t001UserNew != null && !t001UserNew.equals(t001UserOld)) {
                t001UserNew.getT003PharmacistCollection().add(t003Pharmacist);
                t001UserNew = em.merge(t001UserNew);
            }
            for (T006Prescription t006PrescriptionCollectionOldT006Prescription : t006PrescriptionCollectionOld) {
                if (!t006PrescriptionCollectionNew.contains(t006PrescriptionCollectionOldT006Prescription)) {
                    t006PrescriptionCollectionOldT006Prescription.setT003Pharmacist(null);
                    t006PrescriptionCollectionOldT006Prescription = em.merge(t006PrescriptionCollectionOldT006Prescription);
                }
            }
            for (T006Prescription t006PrescriptionCollectionNewT006Prescription : t006PrescriptionCollectionNew) {
                if (!t006PrescriptionCollectionOld.contains(t006PrescriptionCollectionNewT006Prescription)) {
                    T003Pharmacist oldT003PharmacistOfT006PrescriptionCollectionNewT006Prescription = t006PrescriptionCollectionNewT006Prescription.getT003Pharmacist();
                    t006PrescriptionCollectionNewT006Prescription.setT003Pharmacist(t003Pharmacist);
                    t006PrescriptionCollectionNewT006Prescription = em.merge(t006PrescriptionCollectionNewT006Prescription);
                    if (oldT003PharmacistOfT006PrescriptionCollectionNewT006Prescription != null && !oldT003PharmacistOfT006PrescriptionCollectionNewT006Prescription.equals(t003Pharmacist)) {
                        oldT003PharmacistOfT006PrescriptionCollectionNewT006Prescription.getT006PrescriptionCollection().remove(t006PrescriptionCollectionNewT006Prescription);
                        oldT003PharmacistOfT006PrescriptionCollectionNewT006Prescription = em.merge(oldT003PharmacistOfT006PrescriptionCollectionNewT006Prescription);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = t003Pharmacist.getId();
                if (findT003Pharmacist(id) == null) {
                    throw new NonexistentEntityException("The t003Pharmacist with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            T003Pharmacist t003Pharmacist;
            try {
                t003Pharmacist = em.getReference(T003Pharmacist.class, id);
                t003Pharmacist.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The t003Pharmacist with id " + id + " no longer exists.", enfe);
            }
            T001User t001User = t003Pharmacist.getT001User();
            if (t001User != null) {
                t001User.getT003PharmacistCollection().remove(t003Pharmacist);
                t001User = em.merge(t001User);
            }
            Collection<T006Prescription> t006PrescriptionCollection = t003Pharmacist.getT006PrescriptionCollection();
            for (T006Prescription t006PrescriptionCollectionT006Prescription : t006PrescriptionCollection) {
                t006PrescriptionCollectionT006Prescription.setT003Pharmacist(null);
                t006PrescriptionCollectionT006Prescription = em.merge(t006PrescriptionCollectionT006Prescription);
            }
            em.remove(t003Pharmacist);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<T003Pharmacist> findT003PharmacistEntities() {
        return findT003PharmacistEntities(true, -1, -1);
    }

    public List<T003Pharmacist> findT003PharmacistEntities(int maxResults, int firstResult) {
        return findT003PharmacistEntities(false, maxResults, firstResult);
    }

    private List<T003Pharmacist> findT003PharmacistEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(T003Pharmacist.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public T003Pharmacist findT003Pharmacist(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(T003Pharmacist.class, id);
        } finally {
            em.close();
        }
    }

    public int getT003PharmacistCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<T003Pharmacist> rt = cq.from(T003Pharmacist.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
